package one.stand.service;

import com.github.pagehelper.PageHelper;
import one.stand.controller.GoodsController;
import one.stand.dto.ConvertUtils;
import one.stand.dto.Index2Response;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.GoodsApiMapper;
import one.stand.mapper.SeeRecordPublicMapper;
import one.stand.model.Goods;
import one.stand.model.GoodsType;
import one.stand.util.PriceUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cjr
 */
@Service
public class GoodsApiServiceImpl implements GoodsApiService {
    @Autowired
    private GoodsApiMapper goodsApiMapper;
    @Autowired
    private GoodsPhotoService goodsPhotoService;
    @Autowired
    private SeeRecordPublicMapper seeRecordPublicMapper;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsBaseService goodsBaseService;

    @Override
    public List<Index2Response.Goods> categoryGoods(Integer companyId, Integer categoryId) {
        PageHelper.startPage(1, 8);
        List<Goods> goodsModels = goodsApiMapper.selectSearch(companyId, null, categoryId);
        return ConvertUtils.list(goodsModels, model -> {
            GoodsType goodsType = goodsTypeService.get(model.getGoodstypeId());
            String categoryName = goodsType.getGroupName().split(GoodsTypeService.SPLIT)[0];
            return Index2Response.Goods.builder()
                    .id(model.getGoodsId())
                    .name(model.getGoodsName())
                    .price(PriceUtil.getPriceView2(model.getShowPrice() == 1, model.getPrice()))
                    .path(goodsPhotoService.getPath(model.getGoodsId()) + GoodsController.GOODS_IMAGE_SIZE)
                    .visitNum(seeRecordPublicMapper.selectCount(SeeRecordEnums.PRODUCT.getCode(), model.getGoodsId()))
                    .categoryName(categoryName)
                    .categoryName2(goodsType.getName())
                    .hasVr(StringUtils.isNoneBlank(model.getQjVrUrl()))
                    .hasVideo(StringUtils.isNoneBlank(model.getVideoUrl()))
                    .hasAr(goodsBaseService.hasAr(model.getGoodsId()))
                    .build();
        });
    }
}
